﻿//Creado por Laura 26/06/10 Falta terminar que agarre bien los indices y entre al juego
//Terminado

#region Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Engine;
#endregion

namespace Game
{
    class CooperativeMenuScreen : MenuScreen
    {
        ContentManager content;

        PlayerIndex mainIndex;
        PlayerIndex sniperIndex;
        Texture2D theSniper;
        Texture2D theSniperSelected;
        Texture2D mainPlayer;
        Texture2D mainPlayerSelected;
        Vector2 selectedOption;
        PlayerIndex menuIndex;
        bool sniperSelected;
        bool mainSelected;
        

        public CooperativeMenuScreen(PlayerIndex index)
            : base("Co-op")
        {
            
            selectedOption = Vector2.Zero;

            menuIndex = index;

            sniperSelected = false;
            mainSelected = false;

            index = new PlayerIndex();
        }

      

        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            theSniper = content.Load<Texture2D>("Textures/TheSniper1024");
            mainPlayer = content.Load<Texture2D>("Textures/MainPlayer1024");
            theSniperSelected = content.Load<Texture2D>("Textures/TheSniperSelected1024");
            mainPlayerSelected = content.Load<Texture2D>("Textures/MainPlayerSelected1024");
        }

        public override void HandleInput()
        {

            if (Input.IsNewKeyPress(Keys.Up) || Input.IsNewButtonPress(Buttons.DPadUp, menuIndex, out menuIndex)
                && !mainSelected && !sniperSelected)
            {
                if (selectedOption.Y == 0)
                    selectedOption.Y = 384;
                else
                    selectedOption.Y = 0;
            }
            if (Input.IsNewKeyPress(Keys.Down) || Input.IsNewButtonPress(Buttons.DPadDown, menuIndex, out menuIndex)
                && !mainSelected && !sniperSelected)
            {
                if (selectedOption.Y == 0)
                    selectedOption.Y = 384;
                else
                    selectedOption.Y = 0;
            }

            if (!mainSelected && !sniperSelected && selectedOption.Y == 384)
            {
                if (Input.IsNewButtonPress(Buttons.A, menuIndex, out mainIndex) ||
                    Input.IsNewButtonPress(Buttons.Start, menuIndex, out mainIndex))
                {
                    mainSelected = true;
                    JoysticksInfo.SetPlayerIndex(mainIndex);
                }
            }

            if (!mainSelected && !sniperSelected && selectedOption.Y == 0)
            {
                if (Input.IsNewButtonPress(Buttons.A, menuIndex, out sniperIndex) ||
                  Input.IsNewButtonPress(Buttons.Start, menuIndex, out sniperIndex))
               {
                    sniperSelected = true;
                    JoysticksInfo.SetSniperIndex(sniperIndex);
               }
            }



            if (mainSelected && Input.IsNewButtonPress(Buttons.Start, null, out sniperIndex))
            {
                if (sniperIndex != menuIndex && sniperIndex != mainIndex)
                {
                    sniperSelected = true;
                    JoysticksInfo.SetSniperIndex(sniperIndex);
                }
            }
            else
            {
                if (sniperSelected && Input.IsNewButtonPress(Buttons.Start, null, out mainIndex))
                {
                    if (mainIndex != menuIndex && mainIndex != sniperIndex)
                    {
                        mainSelected = true;
                        JoysticksInfo.SetPlayerIndex(mainIndex);
                    }
                }
            }
                
            

            if (mainSelected && sniperSelected)
            {
                AudioManager.StopMenuSound();
                LoadingScreen.Load(ScreenManager, true, mainIndex,
                                   new Level1());
            }


            if (Input.IsMenuCancel(menuIndex, out menuIndex) || Input.IsNewKeyPress(Keys.Escape))
            {
               
                mainSelected = false;
                sniperSelected = false;

                OnCancel(menuIndex);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin();

            if (selectedOption == Vector2.Zero)
            {
                spriteBatch.Draw(theSniperSelected, Vector2.Zero, Color.White);
                spriteBatch.Draw(mainPlayer, new Vector2(0, 384), Color.White);
            }
            else
            {
                spriteBatch.Draw(theSniper, Vector2.Zero, Color.White);
                spriteBatch.Draw(mainPlayerSelected, new Vector2(0, 384), Color.White);
            }

            if (!mainSelected && !sniperSelected)
            {
                spriteBatch.DrawString(ScreenManager.MessageBoxFont, "Player 1 press start",
                    new Vector2(screenWidth / 2 - 150, screenHeight / 2), Color.Red);
            }
            if (mainSelected || sniperSelected)
            {
                spriteBatch.DrawString(ScreenManager.MessageBoxFont, "Player 2 press start",
                    new Vector2(screenWidth / 2 - 150, screenHeight / 2), Color.Red);
            }

            spriteBatch.End();
        }
    }
}
